package com.best.weixin.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.best.core.constants.CommonConstants;
import com.best.core.entity.CommonResult;
import com.best.core.utils.RedisUtil;
import com.best.weixin.service.WeiXinService;

/**
 * @ClassName: WeiXinServiceImpl
 * @Package: com.best.weixin.service.impl
 * @Description: 微信服务业务层实现类
 * @Datetime: 2020/6/10 1:20
 * @Author: nike
 */
@Service
public class WeiXinServiceImpl implements WeiXinService {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public CommonResult verifyCode(String phone, String weixinCode) {
        // 校验参数是否为空
        if (StringUtils.isEmpty(phone)) {
            return CommonResult.buildFail("手机号码不能为空!");
        }
        if (StringUtils.isEmpty(weixinCode)) {
            return CommonResult.buildFail("微信注册码不能为空!");
        }
        // 根据手机号码查询redis返回对应的微信注册码
        String redisCode = redisUtil.getString(CommonConstants.WEIXINCODE_KEY + phone);
        // 判断redis是否存在
        if (StringUtils.isEmpty(redisCode)) {
            return CommonResult.buildFail("微信注册码已经过期!");
        }
        // 比对验证码
        if (!redisCode.equals(weixinCode)) {
            return CommonResult.buildFail("微信注册码不正确!");
        }
        // redis移除验证码
        redisUtil.delKey(CommonConstants.WEIXINCODE_KEY + phone);

        return CommonResult.buildSuccess("验证通过");
    }
}
